package cn.iocoder.yudao.module.picturebook.controller.admin.favorites;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.favorites.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.favorites.FavoritesDO;
import cn.iocoder.yudao.module.picturebook.service.favorites.FavoritesService;

@Tag(name = "绘本系统 - 用户收藏")
@RestController
@RequestMapping("/picturebook/favorites")
@Validated
public class FavoritesController {

    @Resource
    private FavoritesService favoritesService;

    @PostMapping("/create")
    @Operation(summary = "创建用户收藏")
    @PreAuthorize("@ss.hasPermission('picturebook:favorites:create')")
    public CommonResult<Long> createFavorites(@Valid @RequestBody FavoritesSaveReqVO createReqVO) {
        return success(favoritesService.createFavorites(createReqVO))   ;
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户收藏")
    @PreAuthorize("@ss.hasPermission('picturebook:favorites:update')")
    public CommonResult<Boolean> updateFavorites(@Valid @RequestBody FavoritesSaveReqVO updateReqVO) {
        favoritesService.updateFavorites(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户收藏")
    @PreAuthorize("@ss.hasPermission('picturebook:favorites:delete')")
    public CommonResult<Boolean> deleteFavorites(@RequestParam("BookId") Long BookId,@RequestHeader("token") Long userId) {
        //根据用户id和书籍id删除这本书的收藏
        if (favoritesService.deleteFavorites(BookId,userId)) {
            return success(true);
        }else {
            return success(false);
        }
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户收藏")
                @PreAuthorize("@ss.hasPermission('picturebook:favorites:delete')")
    public CommonResult<Boolean> deleteFavoritesList(@RequestParam("ids") List<Long> ids) {
        favoritesService.deleteFavoritesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:favorites:query')")
    public CommonResult<FavoritesRespVO> getFavorites(@RequestParam("id") Long id) {
        FavoritesDO favorites = favoritesService.getFavorites(id);
        return success(BeanUtils.toBean(favorites, FavoritesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户收藏分页")
    @PreAuthorize("@ss.hasPermission('picturebook:favorites:query')")
    public CommonResult<PageResult<FavoritesRespVO>> getFavoritesPage(@Valid FavoritesPageReqVO pageReqVO) {
        PageResult<FavoritesRespVO> favoritesPage = favoritesService.getFavoritesPage(pageReqVO);
        return success(favoritesPage);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户收藏 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:favorites:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFavoritesExcel(@Valid FavoritesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FavoritesRespVO> list = favoritesService.getFavoritesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户收藏.xls", "数据", FavoritesRespVO.class,
                        BeanUtils.toBean(list, FavoritesRespVO.class));
    }
    //根据用户ID查询查询这本书是否被用户收藏
    @GetMapping("/getFavoritesByUserIdAndBookId")
    @Operation(summary = "根据用户ID查询查询这本书是否被用户收藏")
    public CommonResult<Integer> getFavoritesByUserIdAndBookId(@RequestParam("BookId") Long BookId, @RequestHeader("token") Long userId) {
        return CommonResult.success(favoritesService.getFavoritesByUserIdAndBookId(BookId, userId));
    }
}